<?php
class commentService extends commonService{
	public function t(){
		return array('comment','comment_stat');
	}
	
	public function mylist($condition=array(),$extra='',$extra_data=array()){
        $return=array();
        $query="select * from `{$this->t[0]}` where 1 {$extra} order by `id` desc";
        $p=new smart_pager(array($query,$extra_data),$this->m);
        //$p->address('&block_id=%block_id%','block_id',$condition['block_id']);
		foreach($condition as $k=>$v){
			$p->address("&{$k}=%{$k}%",$k,$v);
		}
        if($p->total_num>0){
            $return[]=$this->m->mselect($p->query,$extra_data);
            $return[]=$p;
        }
        return $return;
    }
	//编辑
	public function mod($id){
		$data=$this->model->_f($this->t[0],'*',$id);
		return $data;
	}
	//更新星级
	private function up_star($id,$new_star){
		$query="select `star`,`mid`,`arc_id` from `{$this->t[0]}` where `id`=:id limit 1";
		$r=$this->m->select($query,array('id'=>$id));
		if($r['star'] != $new_star) {
			$star = 'star'.$r['star'];
			$query="update `{$this->t[1]}` set `{$star}`=`{$star}`-1 where `arc_id`=:arc_id and `mid`=:mid limit 1";
			$this->m->query($query,array('arc_id'=>$r['arc_id'],'mid'=>$r['mid']));
			$star = 'star'.$new_star;
			$query="update `{$this->t[1]}` set `{$star}`=`{$star}`+1 where `arc_id`=:arc_id and `mid`=:mid limit 1";
			$this->m->query($query,array('arc_id'=>$r['arc_id'],'mid'=>$r['mid']));
		}
		return true;
	}
	public function mod_($post,$id){
		$result=array();
        
		in_array($post['status'], array(1, 0)) or $post['status'] = 0;
		in_array($post['star'], array(1, 2, 3)) or $post['star'] = 3;
		if($post['reply']){
			$post['replytime']=time();
			$post['reply']=trim($post['reply']);
		}
		$post['content']=trim($post['content']);
		$post['reply']=trim($post['reply']);
		$post['quotation']=trim($post['quotation']);
		
        $vali=validate::check('comment',$post,'mod');
        if($vali){
    		$result[0]=$this->model->_u($this->t[0],$post,$id);
			if($result[0]){
				$this->up_star($id,$post['star']);
			}
    	}else{
    		$result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
    	}
        return $result;
	}
	//删除
	public function del($id){
		$return=array();
		
		$query="select `id`,`mid`,`arc_id`,`star` from `{$this->t[0]}` where `id`=:id limit 1";
		$r=$this->m->select($query,array('id'=>$id));
		if(!$r['id']){
			$return[0]=false;
			return $return;
		}
		
		$star='star'.$r['star'];
		$query="UPDATE `{$this->t[1]}` set `comment`=`comment`-1,`{$star}`=`{$star}`-1 where `arc_id`=:arc_id and `mid`=:mid limit 1";
		$this->m->query($query,array('arc_id'=>$r['arc_id'],'mid'=>$r['mid']));
		
		$return[0]=$this->model->_d($this->t[0],$id);
		return $return;
	}
	//更新status
	public function update_status($id,$status){
		$status=in_array($status,array(1,0))?$status:0;
		$query="update `{$this->t[0]}` set `status`=:status where `id`=:id limit 1";
		return $this->m->query($query,array('status'=>$status,'id'=>$id));
	}
//
}